package com.relytosoft.mizar.analyze.system.services.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.relytosoft.dubhe.common.core.base.page.PageQuery;
import com.relytosoft.dubhe.common.core.object.ResponseResult;
import com.relytosoft.dubhe.common.core.utils.BeanCopyUtils;
import com.relytosoft.dubhe.common.core.utils.CommonUtils;
import com.relytosoft.mizar.analyze.system.api.entity.AyVehicleInfo;
import com.relytosoft.mizar.analyze.system.api.pojo.dto.AyVehicleInfoDto;
import com.relytosoft.mizar.analyze.system.api.pojo.vo.AyVehicleInfoVo;
import com.relytosoft.mizar.analyze.system.services.service.AyVehicleInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * @author lj
 * @date 2022/5/13 16:33
 */
@Api(tags = {"车辆信息"})
@RestController
@RequestMapping("/system/vehicle")
public class AyVehicleInfoController {

    @Resource
    private AyVehicleInfoService ayVehicleInfoService;

    @ApiOperation("车辆信息查询")
    @GetMapping("/list")
    public ResponseResult<Map<String, Object>> list(@RequestParam("condition") String condition,
                                                    @RequestParam("pageQuery") String query) {
        PageQuery pageQuery = JSON.parseObject(query, new TypeReference<PageQuery>() {
        });
        AyVehicleInfoDto ayVehicleInfoDto = JSONObject.parseObject(condition, AyVehicleInfoDto.class);
        AyVehicleInfo ayVehicleInfo = BeanCopyUtils.copy(ayVehicleInfoDto, AyVehicleInfo.class);
        IPage<AyVehicleInfo> page = ayVehicleInfoService.selectAyVehicleInfoPageList(pageQuery, ayVehicleInfo);
        return ResponseResult.ok(CommonUtils.getPageData(page));
    }

    @ApiOperation("新增车辆")
    @PostMapping({"/add"})
    public ResponseResult<Void> add(@RequestBody AyVehicleInfoDto ayVehicleInfoDto) {
        AyVehicleInfo ayVehicleInfo = BeanCopyUtils.copy(ayVehicleInfoDto, AyVehicleInfo.class);
        boolean b = ayVehicleInfoService.addAyVehicleInfo(ayVehicleInfo);
        return b ? ResponseResult.ok("新增车辆成功") : ResponseResult.fail("新增车辆失败");
    }

    @ApiOperation("根据ID查询")
    @GetMapping({"{vehicleId}"})
    public ResponseResult<AyVehicleInfoVo> id(@PathVariable Integer vehicleId) {
        return ResponseResult.ok(ayVehicleInfoService.selectByVehicleId(vehicleId));
    }

    @ApiOperation("删除")
    @DeleteMapping("del")
    public ResponseResult<Void> remove(@RequestBody List<Integer> list) {
        boolean b = ayVehicleInfoService.updateAyVehicleInfoStatus(list);
        return b ? ResponseResult.ok("删除成功") : ResponseResult.fail("删除失败");
    }

    @ApiOperation("修改")
    @PutMapping
    public ResponseResult<Void> update(@RequestBody AyVehicleInfoDto ayVehicleInfoDto) {
        AyVehicleInfo ayVehicleInfo = BeanCopyUtils.copy(ayVehicleInfoDto, AyVehicleInfo.class);
        boolean b = ayVehicleInfoService.updateAyVehicleInfo(ayVehicleInfo);
        return b ? ResponseResult.ok("修改成功") : ResponseResult.fail("修改失败");
    }

    @ApiOperation("模板")
    @GetMapping("import/template")
    public void importTemplate(HttpServletResponse response) {
        ayVehicleInfoService.importTemplate(response);
    }


    @ApiOperation(value = "批量导入车辆信息")
    @PostMapping(value = "import/{ids}", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public ResponseResult<Void> importVehicleInfo(@RequestPart("file") MultipartFile file,
                                                  @PathVariable("ids") List<Integer> ids) {
        //上传过来的excel文件
        ayVehicleInfoService.saveVehicleInfo(file, ayVehicleInfoService, ids);
        return ResponseResult.ok();
    }
}
